<?php
$this->load->view('admin/layout/head', array('title'=>'用户等级管理', 'nav' => 'user'), FALSE);
?>

<div id="container">
    <div>
        <a href="javascript:;" class="btn btn-default add-btn"><span class="glyphicon glyphicon-plus"></span> <?= $this->lang->line('create') ?></a>
    </div>
	<div class="table-container mt-10">
		<table class="table table-bordered table-striped table-hover scroll-table">
			<thead >
				<tr>
					<th width="60" class="text-center">ID</th>
					<th width="60" class="text-center">等级</th>
					<th width="60" class="text-center">名称</th>
					<th width="100" class="text-center">折扣</th>
					<th width="60" class="text-center">要求积分</th>
					<th width="80" class="text-center">操作</th>
				</tr>
			</thead>
			<tbody class="text-center">
				<?php
				foreach ($userLevels as $v) {
				?>
				<tr>
					<td><?php echo $v['id'] ?></td>
					<td><?php echo $v['level'] ?></td>
					<td><?php echo $v['name'] ?></td>
					<td><?php echo $v['discount'] ?></td>
					<td><?php echo $v['credits'] ?></td>
					<td>
						<a href="javascript:;" class="modify-btn" data-id="<?= $v['id'] ?>">编辑</a> |
						<a href="javascript:;" class="del-btn" data-id="<?php echo $v['id'] ?>">删除</a>
				</tr>
				<?php
				}
				?>

			</tbody>
		</table>
	</div>
</div>

<div id="form-div" style="display: none">
	<form id="userLevel-form" class="form-horizontal">
	  <input type="hidden" name="id" value="0">
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">等级：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="level" value="" class="form-control" placeholder="" tautocomplete="off">
	    </div>
	  </div>
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">名称：</label>
	    <div class="col-sm-6">
	    	<input type="text" name="name" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	  <div class="form-group row">
	    <label class="col-sm-3 control-label">折扣</label>
	    <div class="col-sm-6">
	    	<input type="text" name="discount" value="" class="form-control" placeholder="lastname">
	    </div>
	  </div>

	  <div class="form-group row">
	    <label class="col-sm-3 control-label">要求积分</label>
	    <div class="col-sm-6">
	    	<input type="text" name="credits" value="" class="form-control" placeholder="">
	    </div>
	  </div>
	</form>
</div>

<?php
$this->load->view('admin/layout/footer');
?>

<script type="text/javascript">
$(function () {
    var form_html = $('#form-div').html();
    $('#form-div').remove();

    //
    $('.add-btn').on('click', function () {
        var btn = '<a href="javascript:;" class="btn btn-primary user-form-submit">提交</a>';
        var dlg = dialog('<?= $this->lang->line('create') ?>',form_html,btn);

        $('.user-form-submit').off('click');
        $('.user-form-submit').on('click', function () {
            $.post('/admin/userLevel/add', $('#userLevel-form').serialize(), function (json) {
                Toast.success('操作成功',function(){
    				window.location.reload();
    			});
            })
        });
    });
    //修改
    $('.modify-btn').on('click',function(){
        var id = $(this).attr('data-id');
        var btn = '<a href="javascript:;" class="btn btn-primary user-form-submit">提交</a>';
        var dlg = dialog('<?= $this->lang->line('edit') ?>',form_html,btn);

        $.getJSON('/admin/userLevel/detail/'+id, function (json) {
            for(var k in json) {
                $('#userLevel-form input[name='+k+']').val(json[k]);
            }
        });

        $('.user-form-submit').off('click');
        $('.user-form-submit').on('click', function () {
            $.post('/admin/userLevel/edit/'+id, $('#userLevel-form').serialize(), function (json) {
                Toast.success('操作成功',function(){
    				window.location.reload();
    			});
            })
        });
    });

    // 删除
    $('.del-btn').on('click', function () {
        var id = $(this).attr('data-id');

        $.post('/admin/userLevel/delete/'+id, {force: 1}, function (json) {
            Toast.success('操作成功',function(){
                window.location.reload();
            });
        })
    });
});
</script>
